$(document).ready(function() {
	$('#ml_text,#ml_name').addClass("mailinglistmodule_normal");
	$('#ml_text,#ml_name').focus(function() {
		$(this).removeClass("mailinglistmodule_normal").addClass("mailinglistmodule_active");
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	}
	);
	// for form submit
	$("#ml_form").submit(function() {
		// email validation
		myvalue=$('#ml_text').val();
		if (myvalue == "")
		{
			return false;
		}
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(myvalue) == false)
		{
			alert('Địa chỉ email không đúng');
			$("#ml_text").focus();
			return false;
		}
		myvalue = myvalue.replace("'",""); myvalue = myvalue.replace('"',"");
		// okies, here, we call ajax here.
		// we don't want to submit the form
		var t2=Math.floor(Math.random()*99999).toString();
		$.post("/index.php?module=mailinglistmodule&action=register&ajax_action=1&value="+myvalue+"&t2="+t2,
		function(data){
			temp=data.substring(0,1);
			if ((temp =="\r") || (temp == "\n"))
			 data=data.substring(1);
			temp=data.substring(0,1);
			if ((temp =="\r") || (temp == "\n"))
			 data=data.substring(1);
			//alert(data);
	  //data=data.substring(2); // remove enter char
			if ((data.length>t2.length) && (data.substring(0,t2.length) == t2))			
				alert(data.substring(t2.length));
			else
				alert("Network error. Could not submit your email. Please try again");
		});
	// end of ajax call
		return false;
	});
});
